package com.ljx.publisher.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: LiJunXi
 * @Description: 生产者发送消息到MQ return callback 处理
 * @Date: 2024-06-28  22:58
 */
@Slf4j
@Configuration
public class MqConfirmConfig implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        RabbitTemplate rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage message) {
                log.error("收到消息return callback：message:{}, exchange:{}, code:{}, text:{}, routingKey:{}",
                        message.getMessage(), message.getExchange(), message.getReplyCode(),
                        message.getReplyText(), message.getRoutingKey());
            }
        });
    }
}
